package com.onenine.app.adapter;

import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.onenine.app.R;
import com.onenine.app.model.NearbyItem;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.view.NiceImageView;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.ScreenUtils;
import com.onenine.common.util.SizeUtils;

public class ExploreAdapter extends ArrayAdapter<NearbyItem> {

    private FrameLayout.LayoutParams mCoverParams;
    private LayoutInflater mInflater;

    public ExploreAdapter() {
        super(ApUtils.getApp(), R.layout.layout_explore_card_item);
        mInflater = LayoutInflater.from(ApUtils.getApp());
        mCoverParams = new FrameLayout
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (0.65 * ScreenUtils.getScreenHeight()));
    }

    @Override
    public View getView(int position, final View contentView, ViewGroup parent) {
        NearbyItem item = getItem(position);
        NiceImageView ivAvatar = contentView.findViewById(R.id.niv_avatar);
        ivAvatar.setLayoutParams(mCoverParams);
        GlideUtils.loadSrcImage(ApUtils.getApp(), ivAvatar, item.getPhoto());
        GlideUtils.loadSrcImage(ApUtils.getApp(), contentView.findViewById(R.id.niv_avatar1), item.getPhoto());
        TextView tvName = contentView.findViewById(R.id.tv_name);
        tvName.setText(item.getNickName());
        ImageView ivSex = contentView.findViewById(R.id.iv_sex);
        ivSex.setImageResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.COMMUNITY_SEX_ICON, item.getSex()));
        TextView tvSign = contentView.findViewById(R.id.tv_sign);
        if (TextUtils.isEmpty(item.getPersonSign())) {
            //无签名
            tvSign.setHint(R.string.no_special_sign_hint);
            tvSign.setText("");
        } else {
            //有签名
            tvSign.setText(item.getPersonSign());
            tvSign.setHint("");
        }
        FlexboxLayout fbLabel = contentView.findViewById(R.id.fb_label);
        showLabelView(fbLabel, item.getMylabelNames());
        FlexboxLayout fbHobby = contentView.findViewById(R.id.fb_hobby);
        showLabelView(fbHobby, item.getUniqueHobbyLabelNames());
        contentView.setTag(item);
        return contentView;
    }

    /**
     * 显示标签组
     *
     * @param flexboxLayout
     * @param labelNameStr
     */
    public void showLabelView(FlexboxLayout flexboxLayout, String labelNameStr) {
        if (TextUtils.isEmpty(labelNameStr)) {
            return;
        }
        String[] labels = labelNameStr.split(",");
        int labelMargin = SizeUtils.dp2px(5);
        flexboxLayout.removeAllViews();
        for (String label : labels) {
            TextView tvLabel = (TextView) mInflater.inflate(R.layout.layout_my_label_item, null, false);
            tvLabel.setText(label);
            tvLabel.setTextSize(11);
            tvLabel.setBackgroundResource(R.drawable.shape_mine_label_bg);
            tvLabel.setTextColor(ActivityCompat.getColor(getContext(), R.color.c999));
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout
                    .LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, getContext().getResources().getDimensionPixelOffset(R.dimen.dp22));
            layoutParams.setMargins(labelMargin, labelMargin, labelMargin, labelMargin);
            flexboxLayout.addView(tvLabel, -1, layoutParams);
        }
    }

}
